'''
Writen by David
Feb 2nd 2020
'''

import requests
import json
import tkinter as tk
import tkinter.font as tf
from tkinter import ttk

def get_data():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=jQuery341001657575837432268_1581070969707&_=1581070969708'
    headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'}
    res = requests.get(url, headers=headers).text
    a = res.split('jQuery341001657575837432268_1581070969707(')[1].split(')')[0]
    c = json.loads(a)
    data = json.loads(c['data'])
    return data

#获取数据
get_data()
data = get_data()


#创建一个tkinter窗口
window = tk.Tk()
window.title("疫情实时数据查询")
window.geometry("1100x450")
window.resizable(0,0)
#创建多个text部件，用来显示总体的数据，调用数据方法是在网上学习后自己读json文件写的
t1=tk.Text(window,width=80,height=1,font=tf.Font(size=15))
t1.pack(side=tk.TOP)
t1.insert("end","         "+"统计截至时间："+str(data['lastUpdateTime'])+"        "+"数据来源：腾讯实时疫情")

t2=tk.Text(window,width=100,height=1,font=tf.Font(size=15),foreground="red")
t2.pack(side=tk.TOP)
t2.insert("end","                       "+"全国确诊人数："+str(data['chinaTotal']['confirm'])+"        "+"相较于昨天新增确诊人数："+str(data['chinaAdd']['confirm']))


t3=tk.Text(window,width=100,height=1,font=tf.Font(size=15),foreground="yellow")
t3.pack(side=tk.TOP)
t3.insert("end","                       "+"全国疑似病例："+str(data['chinaTotal']['suspect'])+"        "+"相较于昨天新增疑似人数："+str(data['chinaAdd']['suspect']))

t4=tk.Text(window,width=100,height=1,font=tf.Font(size=15),foreground="green")
t4.pack(side=tk.TOP)
t4.insert("end","                       "+"全国治愈人数："+str(data['chinaTotal']['heal'])+"         "+"相较于昨天新增治愈人数："+str(data['chinaAdd']['heal']))

t5=tk.Text(window,width=100,height=1,font=tf.Font(size=15),foreground="gray")
t5.pack(side=tk.TOP)
t5.insert("end","                       "+"全国死亡人数："+str(data['chinaTotal']['dead'])+"          "+"相较于昨天新增死亡人数："+str(data['chinaAdd']['dead']))

t6=tk.Text(window,width=80,height=0.6,font=tf.Font(size=15),foreground="black")
t6.pack(side=tk.TOP)
t6.insert("end","请在下方输入您要查询的省份（或直辖市）例：北京")

#一个entry输入框，用来获取输入的省份名称
e1=tk.Entry(window,width=48)
e1.pack(side=tk.TOP)

#滚动条，因为下面用的tree表格一次只能显示十行，但有的省大城市多于10个，所以用滚动条上下滑动操控tree表格
scrollBar = tk.Scrollbar(window)
scrollBar.pack(side=tk.RIGHT,fill = tk.Y)
#创建一个tree表格，用来显示一个省内的疫情分布情况
tree = ttk.Treeview(window,show='headings',yscrollcommand=scrollBar.set)
#flag是为后面重置表格用的
flag=0
#该方法是用来绘制表格，在最后按下button按钮后执行
def print_children():
    global flag
    #定义一个重置函数，程序打开后可以多次输入省的名称，不用重复运行程序
    def delButton(tree):
        x=tree.get_children()
        for item in x:
            tree.delete(item)
    #保证第一次按按钮时不删除表格，从第二次以后flag==1，再执行重置程序    
    if flag!=0:
        delButton(tree)


    
    data = get_data()['areaTree'][0]['children']
    path = str(e1.get())
    path_data = []
    path_china = []
    #遍历数据，放到列表里
    for i in data:
        path_china.append(i['name'])
        path_data.append(i['children'])
    #如果数据库中有输入的省，执行绘制表格
    if path in path_china:
        num = path_china.index(path)
        data_path = path_data[num]

        #画表头
        ttk.Treeview.column(tree,"#0",width=100,anchor="center")
        tree["columns"] = ('地区','累计确诊人数','相较于昨日新增确诊人数','累计疑似病例','确诊减少人数','累计治愈人数','治愈率','累计死亡人数','死亡率') 
        tree.column('地区', width=80)
        tree.column('累计确诊人数', width=100)
        tree.column('相较于昨日新增确诊人数', width=150)
        tree.column('累计疑似病例', width=100)
        tree.column('确诊减少人数', width=100)
        tree.column('累计治愈人数', width=100)
        tree.column('治愈率', width=100)
        tree.column('累计死亡人数', width=100)
        tree.column('死亡率', width=100)

        tree.heading('地区', text='地区')
        tree.heading('累计确诊人数', text='累计确诊人数') 
        tree.heading('相较于昨日新增确诊人数', text='相较于昨日新增确诊人数') 
        tree.heading('累计疑似病例', text='累计疑似病例') 
        tree.heading('确诊减少人数', text='确诊减少人数') 
        tree.heading('累计治愈人数', text='累计治愈人数') 
        tree.heading('治愈率', text='治愈率') 
        tree.heading('累计死亡人数', text='累计死亡人数') 
        tree.heading('死亡率', text='死亡率')
        h=0
        #插入行
        for i in data_path:
            name = i['name']
            today = i['today']
            total = i['total']
            tree.insert("",h, text = name, values=(name,str(total['confirm']), str(today['confirm']), str(total['suspect']), str(today['confirmCuts']), str(total['heal']), str(total['healRate']), str(total['dead']), str(total['deadRate'])))
            h+=1
            flag = 1
        tree.pack()


        scrollBar.config(command=tree.yview)
    #如果没有输入的省，显示
    else:
        t6.insert("end","无此省（或直辖市），请重启程序")



b1=tk.Button(window,width=68,text="点击查看结果",command=print_children)
b1.pack(side=tk.TOP)

window.mainloop()
